package com.higold.cloud.ui.fragment

import android.graphics.Typeface
import android.os.Bundle
import android.support.v7.widget.GridLayoutManager
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.entity.MultiItemEntity
import com.higold.cloud.R
import com.higold.cloud.base.BaseFragment
import com.higold.cloud.mvp.contract.AchieveContract
import com.higold.cloud.mvp.model.bean.AchieveBean
import com.higold.cloud.mvp.model.bean.Data
import com.higold.cloud.mvp.presenter.AchievePresenter
import com.higold.cloud.net.exception.ErrorStatus
import com.higold.cloud.showToast
import com.higold.cloud.ui.adapter.AchievedAdapter
import kotlinx.android.synthetic.main.fragment_achieved.*



/**
 *    author : lxz
 *    date   : 2019/8/14 9:34
 *    desc   :
 */
class AchievedFragment : BaseFragment(), AchieveContract.View {

    private val mPresenter by lazy { AchievePresenter() }
    private var mTitle: String? = null
    private lateinit var adapter: AchievedAdapter
    private lateinit var list: ArrayList<MultiItemEntity>
    private var isFirst: Boolean = true
    private var isShow: Boolean = true

    companion object {
        fun getInstance(title: String): AchievedFragment {
            val fragment = AchievedFragment()
            val bundle = Bundle()
            fragment.arguments = bundle
            fragment.mTitle = title
            return fragment
        }
    }

    init {
        mPresenter.attachView(this)
    }

    override fun getLayoutId(): Int = R.layout.fragment_achieved

    override fun lazyLoad() {
        mPresenter.getAchieveInfo(0)
    }

    override fun setAchieveInfo(achieveBean: AchieveBean) {
        multipleStatusView.showContent()
        if (isFirst) {
            list = generateData(achieveBean.data)
            adapter = AchievedAdapter(list)
            adapter.openLoadAnimation(BaseQuickAdapter.SLIDEIN_BOTTOM)

            adapter.setHasStableIds(true)
            rv_achieve.setItemViewCacheSize(list.size)

            val manager = GridLayoutManager(activity, 3)
            manager.spanSizeLookup = object : GridLayoutManager.SpanSizeLookup() {
                override fun getSpanSize(position: Int): Int {
                    return manager.spanCount
                }
            }

            rv_achieve.adapter = adapter
            // important! setLayoutManager should be called after setAdapter
            rv_achieve.layoutManager = manager
            adapter.expandAll()
        } else {
            adapter.replaceData(generateData(achieveBean.data))
            adapter.notifyDataSetChanged()
            adapter.expandAll()
            rv_achieve.smoothScrollToPosition(0)
        }
    }

    override fun showError(errorMsg: String, errorCode: Int) {
        showToast(errorMsg)
        if (errorCode == ErrorStatus.NETWORK_ERROR) {
            multipleStatusView.showNoNetwork()
        } else {
            multipleStatusView.showError()
        }
    }

    override fun showLoading() {
        multipleStatusView.showLoading()
    }

    override fun dismissLoading() {
    }

    override fun onDestroy() {
        super.onDestroy()
        mPresenter.detachView()
    }

    override fun initView() {

        iv_show.setBackgroundResource(R.drawable.ic_classify)
        iv_show.setOnClickListener {
            if (isShow) {
                iv_show.setBackgroundResource(R.drawable.ic_classify_n)
                isShow = false
                for (i in 0 until (list.size + 1) * 3) {
                    if (adapter.getItemViewType(i) == AchievedAdapter.TYPE_LEVEL_0) {
                        adapter.collapse(i)
                    }
                }
            } else {
                iv_show.setBackgroundResource(R.drawable.ic_classify)
                isShow = true
                for (i in 0 until (list.size + 1) * 3) {
                    if (adapter.getItemViewType(i) == AchievedAdapter.TYPE_LEVEL_0) {
                        adapter.expand(i)
                    }
                }
            }
        }

        tv_current_month.setOnClickListener {
            mPresenter.getAchieveInfo(0)
            tv_current_month.typeface = Typeface.defaultFromStyle(Typeface.BOLD)
            tv_cumulative.typeface = Typeface.defaultFromStyle(Typeface.NORMAL)
        }

        tv_cumulative.setOnClickListener {
            mPresenter.getAchieveInfo(1)
            tv_cumulative.typeface = Typeface.defaultFromStyle(Typeface.BOLD)
            tv_current_month.typeface = Typeface.defaultFromStyle(Typeface.NORMAL)
        }

    }

    private fun generateData(list: ArrayList<Data>): ArrayList<MultiItemEntity> {
        isFirst = false
        val res = ArrayList<MultiItemEntity>()
        for (i in 0 until list.size) {
            val data = list[i]
            for (j in 0 until data.detail.size) {
                data.addSubItem(data.detail[j])
            }
            res.add(data)
        }
        return res
    }

}